package com.lsyw.intergrate.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsyw.intergrate.entity.SysNo;
import com.lsyw.intergrate.mapper.SysNoMapper;
import com.lsyw.intergrate.service.SysNoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;

/**
 * <p>
 * 系统编号 服务实现类
 * </p>
 *
 * @author 刘磊
 * @since 2023-05-11
 */
@Service
public class SysNoServiceImpl extends ServiceImpl<SysNoMapper, SysNo> implements SysNoService {

    @Autowired
    private SysNoMapper sysNoMapper;

    @Override
    public String getNum(String id) {
        SysNo sysNo = this.sysNoMapper.selectById(id);
        String no = "";
        //没有值
        if (sysNo == null) {
            sysNo = new SysNo();
            sysNo.setId(id);
            sysNo.setNo(1);
            sysNo.setDelFlag(0);
            this.sysNoMapper.insert(sysNo);
            no = id + geFourNumber(sysNo.getNo());
        } else {
            sysNo.setNo(sysNo.getNo() + 1);
            this.sysNoMapper.updateById(sysNo);
            no = id + geFourNumber(sysNo.getNo());
        }
        return no;
    }

    /**
     * 数字前面自动补零
     *
     * @param number 数字
     * @return
     */
    public static String geFourNumber(int number) {
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(6);
        formatter.setGroupingUsed(false);
        return formatter.format(number);
    }

}
